﻿var mediate = {
    init: function() {
        var that = this;
        that.initList();
        that.initDetail();
    },

    initList: function(page) {
        var that = this;
        $.ajax({
            url: COMMONURL + "api/BidDispute/GelBidDisputeList?token=" + $.cookie('token'),
            type: "post",
            dataType: "json",
            data: {
                page: page,
                pagesize: 20
            }
        }).done(function(redata) {
            if (redata.recode !== '0000') return;
            if (!redata.mod) return;
            if (redata.mod.DataObj.length <= 0) return;
             var odata = redata.mod.DataObj;
            //alert(that.format(odata[0].end_bid_time));
                for (var i = 0, length = odata.length; i < length; i++) {
                    odata[i].end_bid_time = that.format(odata[i].end_bid_time);
                    odata[i].consignee_time = that.format(odata[i].consignee_time);
                }
            var compiled = _.template($("#mediateTpl1").html());
            $("#mediate1").html(compiled(redata.mod.DataObj));
            // 显示分页
            $("#recodePage").show().jqPaginator({
                //totalPages: Math.ceil(redata.mod.TotalNum/20),
                totalPages: redata.mod.TotalPageCount,
                visiblePages: 20,
                currentPage: page,
                first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
                prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
                next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
                last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
                page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
                onPageChange: function(num, type) {
                    if (type == 'change') {
                        that.initList(num);
                    }
                }
            });
        })
    },
    format: function(nS) {
        //return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
        if (!nS) return null;
        var date = new Date(nS * 1000);
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var d = date.getDate();
        var hours = date.getHours();
        var minutes = date.getMinutes();
        var seconds = date.getSeconds();
        return year + '-' + month + '-' + d + ' ' + hours + ':' + minutes;
    },
    initDetail: function() {
        $('#mediate1').on('click', '.J_detail', function() {
            // 初始化数据
            var id = $(this).closest('[data-id]').data('id');
            // todo 用于测试
            // id = '1';

            $.ajax({
                url: COMMONURL + "api/BidDispute/Get?token=" + $.cookie('token') + '&id=' + id,
                type: "post",
                dataType: "json",
                data: {

                }
            }).done(function(redata) {
                if (redata.recode == '0000' && redata.mod.length > 0) {
                    var compiled = _.template($("#mediateDetailTpl").html());
                    $("#mediateDetail").html(compiled(redata.mod[0]));
                    // 显示窗口
                    $('#mediateDetailModal').modal();
                } else {

                }

            })
        });
    }
}

mediate.init();
